package ace.cmp.easycaptcha.core.service.impl;

import ace.cmp.easycaptcha.core.properties.EasyCaptchaProperties;
import ace.cmp.easycaptcha.core.service.EasyCaptchaFactory;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.SneakyThrows;

/**
 * @author caspar
 * @date 2023/3/21 16:43
 */
public class EasyCaptchaFactoryImpl implements EasyCaptchaFactory {

  @SneakyThrows
  @Override
  public Captcha create(EasyCaptchaProperties properties) {
    Captcha captcha = this.createByCaptchaType(properties.getCaptchaType());
    captcha.setCharType(properties.getCaptchaCharType().getCode());
    captcha.setFont(properties.getFont().getCode());
    captcha.setHeight(properties.getHeight());
    captcha.setWidth(properties.getWidth());
    captcha.setLen(
        EasyCaptchaProperties.CaptchaTypeEnum.ARITHMETIC_CAPTCHA.equals(properties.getCaptchaType())
            ? properties.getArithmeticLength()
            : properties.getLength());
    return captcha;
  }

  private Captcha createByCaptchaType(EasyCaptchaProperties.CaptchaTypeEnum captchaType) {
    if (EasyCaptchaProperties.CaptchaTypeEnum.ARITHMETIC_CAPTCHA.equals(captchaType)) {
      return new ArithmeticCaptcha();
    } else if (EasyCaptchaProperties.CaptchaTypeEnum.CHINESE_CAPTCHA.equals(captchaType)) {
      return new ChineseCaptcha();
    } else {
      return new SpecCaptcha();
    }
  }
}
